package com.juqimiao.raccoon.commodity.convertor;

import com.juqimiao.raccoon.commodity.appobj.CategoryGroupAO;
import com.juqimiao.raccoon.commodity.appobj.CategoryGroupListAO;
import com.juqimiao.raccoon.commodity.tunnel.database.dataobject.CategoryGroupDO;
import com.juqimiao.linn.convertor.ConvertorI;
import com.juqimiao.raccoon.commodity.domain.category.CategoryGroup;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;

import org.springframework.stereotype.Component;

/**
 * @author Collin Cai
 */
@Component
public class CategoryGroupConverter implements ConvertorI {


    private ModelMapper modelMapper = new ModelMapper();

    public CategoryGroupConverter() {
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        modelMapper.createTypeMap(CategoryGroupDO.class, CategoryGroupListAO.class)
                .addMappings(mapper -> mapper.map(CategoryGroupDO::getId, CategoryGroupListAO::setId));
    }

    public CategoryGroupListAO dataObjectToAppObject(CategoryGroupDO categoryGroupDO) {

        if (categoryGroupDO != null) {
            CategoryGroupListAO categoryGroupListAO = new CategoryGroupListAO();
            modelMapper.map(categoryGroupDO, categoryGroupListAO);
            return categoryGroupListAO;
        }
        return null;
    }

    public CategoryGroupDO toDataObject(CategoryGroup group) {
        CategoryGroupDO categoryGroupDO = null;
        if (group != null) {
            categoryGroupDO = new CategoryGroupDO();
            modelMapper.map(group, categoryGroupDO);
        }
        return categoryGroupDO;
    }


    public CategoryGroupDO appObjectToDataObject(CategoryGroupAO categoryGroupAO) {
        CategoryGroupDO categoryGroupDO = new CategoryGroupDO();
        if (categoryGroupAO != null) {
            modelMapper.map(categoryGroupAO, categoryGroupDO);
        }

        return categoryGroupDO;
    }

    public CategoryGroupAO toApplicationObject(CategoryGroupDO categoryGroupDO) {
        CategoryGroupAO categoryGroupAo = null;
        if (categoryGroupDO != null) {
            categoryGroupAo = new CategoryGroupAO();
            modelMapper.map(categoryGroupDO, categoryGroupAo);
        }
        return categoryGroupAo;
    }

    public CategoryGroup toEntity(CategoryGroupAO categoryGroupAO) {

        CategoryGroup categoryGroup = null;
        if (categoryGroupAO != null) {
            categoryGroup = new CategoryGroup();
            modelMapper.map(categoryGroupAO, categoryGroup);
        }
        return categoryGroup;
    }

    public CategoryGroup toEntity(CategoryGroupDO categoryGroupDo) {
        CategoryGroup categoryGroup = null;
        if (categoryGroupDo != null) {

            categoryGroup = new CategoryGroup();
            modelMapper.map(categoryGroupDo, categoryGroup);
        }
        return categoryGroup;
    }
}
